import { defineStore } from "pinia";
import {getShow, getShowOut} from "@/services/setting.ts";
import { getUser } from "@/utils";
import type { SettingType } from "@/types/SettingType.ts";

type State = {
    setting: SettingType
}

const defaultSetting = {
    createBy: '',
    createTime: '',
    enableCrumb: false,
    enableFoot: false,
    enableGray: false,
    enableTab: false,
    id: '',
    menuColor: '',
    menuLayout: 0,
    pageIsCompact: 0,
    stringStyle: 0,
    sysColor: '',
    topColor: '',
    updateBy: '',
    updateTime: '',
    userId: ''
}

const useSettingStore = defineStore('setting', {
    state: (): State => ({
        setting: {
            createBy: '',
            createTime: '',
            enableCrumb: false,
            enableFoot: false,
            enableGray: false,
            enableTab: false,
            id: '',
            menuColor: '',
            menuLayout: 0,
            pageIsCompact: 0,
            stringStyle: 0,
            sysColor: '',
            topColor: '',
            updateBy: '',
            updateTime: '',
            userId: ''
        }
    }),
    actions: {
        setSetting() {
            const user = getUser();
            if (user.id) {
                getShow(user.id).then(res => {
                    if (res.code === 200) {
                        if (res.data) {
                            this.setting = res.data
                        } else {
                            this.clearSetting()
                        }
                    }
                })
            }
        },
        setOutSetting() {
            const user = getUser();
            if (user.id) {
                getShowOut(user.id).then(res => {
                    if (res.code === 200) {
                        if (res.data) {
                            this.setting = res.data
                        } else {
                            this.clearSetting()
                        }
                    }
                })
            }
        },
        getSetting(): State['setting'] {
            return this.setting
        },
        clearSetting() {
            this.setting = defaultSetting
        },

        updateSetting(data: SettingType) {
            this.setting = data
        },

        // 页面紧凑
        getPageIsCompact() {
            // 0 默认 1紧凑
            if (this.setting.pageIsCompact === 0) {
                return 'medium'
            }
            return 'mini'
        },

        // 页脚显示
        getFooterShow() {
            return this.setting.enableFoot
        },
    },
})

export default useSettingStore